#include "mode_display.h"
/*******************************************************************
 * @brief    设置ws281x颜色32位
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
uint32_t ws281x_color(uint8_t red, uint8_t green, uint8_t blue)
{
	return green << 16 | red << 8 | blue;
}


/*******************************************************************
 * @brief    
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
uint32_t ws281x_wheel(uint8_t wheelPos) 
{
	wheelPos = 255 - wheelPos;
	if(wheelPos < 85) 
	{
		return ws281x_color(255 - wheelPos * 3, 0, wheelPos * 3);
	}
	if(wheelPos < 170)
	{
		wheelPos -= 85;
		return ws281x_color(0, wheelPos * 3, 255 - wheelPos * 3);
	}
	wheelPos -= 170;
	return ws281x_color(wheelPos * 3, 255 - wheelPos * 3, 0);
}


/*******************************************************************
 * @brief    填充ws281x颜色
 * @Author   
 * @DateTime 2024-04-01
 * @param    
 * @return   
 *******************************************************************/
void ws281x_set_pixel_color(uint16_t n ,uint32_t GRBcolor,u16 length,u8 *uart_rx_buffer)
{
	if(n < length)
	{   
       uart_rx_buffer[n] = (u8)((GRBcolor>>16)&0xFF);
       uart_rx_buffer[n+1] =(u8)((GRBcolor>>8)&0xFF);
       uart_rx_buffer[n+2] =(u8)(GRBcolor&0xFF);
	}
}

void disp_clear_light(u16 length)
{
    u8 buf[300];
    bzero(buf,300);
    logic_send_light_msg(buf);
}

u8 light_detection_ws2812(u16 i) //灯带检测函数
{
    u8 buf[300];
    bzero(buf,300);
    if(1==i)
    {
        
        for (int j = 0; j <= get_light_length(); j += 1)  //算出每个灯珠的颜色
        { 
            buf[j*3+0] = 255; //R
            buf[j*3+1] = 0; //G
            buf[j*3+2] = 0; //B
        }
    }
    else if(2==i)
    {
        for (int j = 0; j <= get_light_length(); j += 1)  //算出每个灯珠的颜色
        { 
            buf[j*3+0] = 0; //R
            buf[j*3+1] = 255; //G
            buf[j*3+2] = 0; //B
        }
    }
    else if(3==i)
    {
        for (int j = 0; j <= get_light_length(); j += 1)  //算出每个灯珠的颜色
        { 
            buf[j*3+0] = 0; //R
            buf[j*3+1] = 0; //G
            buf[j*3+2] = 255; //B
        }
    }
    logic_send_light_msg(buf);
    return 0;
}

void light_rainbow_ws2812() //彩虹灯效
{   
    static u16 j =0;
    static u16 q = 0;
    u8 buf[300];

    q++;
    if(q > 2)
    {
        bzero(buf,300);
        
        for (uint16_t i=0; i < get_light_length()*3; i=i+3) 
        {
            ws281x_set_pixel_color(i, ws281x_wheel((i+j)%255),get_light_length()*3,buf);    //turn every third pixel on
        }

        logic_send_light_msg(buf);
        q=0;
        j=j+2;
    }
    
    if(j > 256)
    {
        j=0;
    }

}





